package com.tzbank.wisdom.controller;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.ResultUtil;
import com.tzbank.wisdom.service.TellerService;
import com.tzbank.wisdom.service.TradeLogService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 渠道控制器
 *
 */
@Api(tags={"渠道接口控制器"})
@Slf4j
@Controller
@RequestMapping(value = "/channel")
public class ChannelController {
	
	@Autowired
    private TellerService tellerService;
	
	@Autowired
    private TradeLogService tradeLogService;
	
	/**
	 * 渠道公共处理方法
	 * @param themeId
	 * @return
	 */
    @ResponseBody
    @RequestMapping(value = "/common", method= {RequestMethod.POST},produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value = "渠道公共处理接口", notes = "渠道公共处理接口")
    @ApiImplicitParams({ @ApiImplicitParam(name = "actionType", value = "动作类型")})
    public Map<String,Object> common(@RequestBody Map<String,String> reqBody)  {
    	String actionType = reqBody.get("actionType");
    	
    	log.info("==============>入参体:" + JSONObject.fromObject(reqBody));
    	
    	tradeLogService.addTradeLog(reqBody);
    	
    	if(StringUtils.isNotBlank(actionType)) {
    		if("contact".equals(actionType)) {
    			return contact(reqBody);
    		}else if("transferHandle".equals(actionType)){
    			//交易模块
    			log.info("===========》进入渠道转账操作《========================");
    			
    			//判断是否是视频teller的  进行转账结果推送
    			String serialNum = reqBody.get("serialNum");
    			String tradeNo = reqBody.get("tradeNo");
    			if(StringUtils.isNotBlank(serialNum) && StringUtils.isNotBlank(tradeNo)) {
    				String result = tellerService.sendTellerResult(reqBody);
    				log.info("teller交易结果推送" + result);
    			}
    			
    			return ResultUtil.resultMap(CodeUtil.CODE_200.getName(), CodeUtil.CODE_200.getCode(), null);
    		}else{
    			return ResultUtil.resultMap(CodeUtil.CODE_200.getName(), CodeUtil.CODE_200.getCode(), null);
    		}
    	}else {
    		return ResultUtil.resultMap(CodeUtil.CODE_10000.getName(), CodeUtil.CODE_10000.getCode(), null);
    	}
    }
    
	/**
	 * 发送验证码短信
	 * @param themeId
	 * @return
	 */
    @ResponseBody
    @RequestMapping(value = "/sendCodeMsg", method= {RequestMethod.POST},produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value = "短信验证码发送", notes = "短信验证码发送接口")
    @ApiImplicitParams({ @ApiImplicitParam(name = "telephone", value = "手机号码"),@ApiImplicitParam(name = "opType", value = "操作类型") })
    public Map<String,Object> sendCodeMsg(@RequestBody Map<String,String> map)  {
    	String telephone = map.get("telephone");
    	if("18888888888".equals(telephone)) {
    		return ResultUtil.resultMap(CodeUtil.CODE_200.getName(), CodeUtil.CODE_200.getCode(), null);
    	}else {
    		return ResultUtil.resultMap(CodeUtil.CODE_10002.getName(), CodeUtil.CODE_10002.getCode(), null);
    	}
    }
    
    /**
	 * 短信验证接口
	 * @param themeId
	 * @return
	 */
    @ResponseBody
    @RequestMapping(value = "/verifyCode", method= {RequestMethod.POST},produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value = "验证码验证", notes = "短信验证接口")
    @ApiImplicitParams({ @ApiImplicitParam(name = "verificationCode", value = "验证码"),@ApiImplicitParam(name = "telephone", value = "手机号码") })
    public Map<String,Object> verifyCode(@RequestBody Map<String,String> map) {
    	String verificationCode = map.get("verificationCode");
    	if("666666".equals(verificationCode)) {
    		return ResultUtil.resultMap(CodeUtil.CODE_200.getName(), CodeUtil.CODE_200.getCode(), null);
    	}else {
    		return ResultUtil.resultMap(CodeUtil.CODE_10001.getName(), CodeUtil.CODE_10001.getCode(), null);
    	}
    }
    
    /**
	 * 密码验证
	 * @param themeId
	 * @return
	 */
    @ResponseBody
    @RequestMapping(value = "/passwordVerify", method= {RequestMethod.POST},produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value = "密码验证", notes = "密码验证接口")
    @ApiImplicitParams({ @ApiImplicitParam(name = "password", value = "密码"),@ApiImplicitParam(name = "cardNumber", value = "卡号") })
    public Map<String,Object> passwordVerify(@RequestBody Map<String,String> map) {
		return ResultUtil.resultMap(CodeUtil.CODE_200.getName(), CodeUtil.CODE_200.getCode(), null);
    }
    
    /**
	 * 用户联系人
	 * @param themeId
	 * @return
	 */
    @ResponseBody
    @RequestMapping(value = "/contact", method= {RequestMethod.POST},produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value = "用户联系人", notes = "用户联系人接口")
    @ApiImplicitParams({ @ApiImplicitParam(name = "customId", value = "客户号") })
    public Map<String,Object> contact(@RequestBody Map<String,String> map) {
    	JSONObject contact1 = new JSONObject();
    	contact1.element("cardNum", "5645612313546");
    	contact1.element("name", "张阿三");
    	contact1.element("issuingBank", "工商银行");
    	
    	JSONObject contact2 = new JSONObject();
    	contact2.element("cardNum", "666854512");
    	contact2.element("name", "张阿四");
    	contact2.element("issuingBank", "台州银行");
    	
    	JSONObject contact3 = new JSONObject();
    	contact3.element("cardNum", "3658974125");
    	contact3.element("name", "李四");
    	contact3.element("issuingBank", "泰隆银行");
    	
    	JSONArray jsonArr = new JSONArray();
    	jsonArr.add(contact1);
    	jsonArr.add(contact2);
    	jsonArr.add(contact3);
    	
    	
    	JSONObject contactlist = new JSONObject();
    	contactlist.element("contactlist", jsonArr);
    	
		return ResultUtil.resultMap(CodeUtil.CODE_200.getName(), CodeUtil.CODE_200.getCode(), contactlist);
    }
    
    /**
	 * 客户信息查询
	 * @param themeId
	 * @return
	 */
    @ResponseBody
    @RequestMapping(value = "/queryCustom", method= {RequestMethod.POST},produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value = "客户信息查询", notes = "客户信息查询接口")
    @ApiImplicitParams({ @ApiImplicitParam(name = "customId", value = "客户号") })
    public Map<String,Object> findCustom(@RequestBody Map<String,String> map) {
    	String customID = map.get("customId");
    	System.out.println("customId" + customID);
    	JSONObject contact1 = new JSONObject();
    	contact1.element("customId", customID);
    	contact1.element("customName", "王铁");
    	contact1.element("phone", "1888656856585");
		return ResultUtil.resultMap(CodeUtil.CODE_200.getName(), CodeUtil.CODE_200.getCode(), contact1);
    }
    
}
